<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜单名称" prop="name">
        <el-input prefix-icon="el-icon-search" v-model="queryParams.name" placeholder="请输入菜单名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="menuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="分组名称" width="80" align="center" prop="groupName"/>
      <el-table-column label="组序" width="60" align="center" prop="groupOrderNum"/>
      <el-table-column label="菜单名称" width="100" align="center" prop="name"/>
      <el-table-column label="顺序" width="60" align="center" prop="orderNum"/>
      <el-table-column label="路由地址" width="100" show-tooltip-when-overflow align="center" prop="path"/>
      <el-table-column label="图标类型" width="80px" align="center" prop="iconType"/>
      <el-table-column label="图标" width="50">
        <template v-slot="scope">
          <i v-if="scope.row.isCustom === 1" style="font-size: 25px" :class="scope.row.iconType"></i>
          <i v-else style="font-size: 25px" :class="'uniui-'+scope.row.iconType"></i>
        </template>
      </el-table-column>
      <el-table-column label="颜色" width="50">
        <template v-slot="scope">
          <el-color-picker v-model="scope.row.iconClass" size="mini"></el-color-picker>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="65px" align="center">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="路由参数" align="center" prop="query"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" width="120px" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改app菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="分组名称" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入分组名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分组排序" prop="groupOrderNum">
              <el-input-number v-model="form.groupOrderNum" :min="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入菜单名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单顺序" prop="orderNum">
              <el-input-number v-model="form.orderNum" :min="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="路由地址" prop="path">
              <el-input v-model="form.path" placeholder="请输入路由地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路由参数" prop="query">
              <el-input v-model="form.query" placeholder="请输入路由参数"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="图标类型" prop="isCustom">
              <el-radio-group v-model="form.isCustom" @input="changeRadio">
                <el-radio :label="1">阿里扩展</el-radio>
                <el-radio :label="0">UniApp默认</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图标颜色">
              <el-color-picker v-model="form.iconClass" color-format="hex" :show-alpha="false" :predefine="predefineColors">
              </el-color-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="选择图标" prop="iconType">
              <el-input v-if="form.isCustom === 1" v-model="form.iconType" placeholder="选择扩展图标">
                <el-button slot="prepend" :icon="prefixIcon">
                </el-button>
                <el-button slot="append" icon="el-icon-thumb" @click="openExtIconsDialog(1)">
                  搜索
                </el-button>
              </el-input>
              <el-input v-else v-model="form.iconType" placeholder="选择uniApp默认图标">
                <el-button slot="prepend" :icon="prefixIcon">
                </el-button>
                <el-button slot="append" icon="el-icon-search" @click="openExtIconsDialog(0)">
                  搜索
                </el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="外链" prop="isFrame">
              <el-radio-group v-model="form.isFrame">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否缓存" prop="isCache">
              <el-radio-group v-model="form.isCache">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="启用" prop="status">
          <el-switch v-model="form.status" :active-value=1 :inactive-value=0 active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <ext-icons-dialog :visible.sync="extIconsVisible" :current="currentIconModelValue" @select="setIcon"/>
    <uni-app-icons-dialog :visible.sync="uniAppIconsVisible" :current="currentIconModelValue" @select="setIcon"/>
  </div>
</template>

<script>
import { listAppMenu, getAppMenu, delAppMenu, addAppMenu, updateAppMenu } from '@/api/eye/appMenu'
import ExtIconsDialog from '../components/ExtIconsDialog/ExtIconsDialog'
import UniAppIconsDialog from '../components/UniAppIconsDialog/UniAppIconsDialog'
import '@/assets/font/index.css'

export default {
  name: 'appMenu',
  components: { ExtIconsDialog, UniAppIconsDialog },
  data() {
    return {
      prefixIcon: 'el-icon-loading',
      extIconsVisible: false,
      uniAppIconsVisible: false,
      currentIconModelValue: null,
      predefineColors: [
        '#ff4500',
        '#ff8c00',
        '#ffd700',
        '#90ee90',
        '#00ced1',
        '#1e90ff',
        '#c71585',
        'rgba(255, 69, 0, 0.68)',
        'rgb(255, 120, 0)',
        'hsv(51, 100, 98)',
        'hsva(120, 40, 94, 0.5)',
        'hsl(181, 100%, 37%)',
        'hsla(209, 100%, 56%, 0.73)'
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // app菜单表格数据
      menuList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        groupName: null,
        path: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeRadio(v){
      console.log(v)
      this.form.iconType = ''
      this.prefixIcon = 'el-icon-loading'
    },
    openExtIconsDialog(model) {
      console.log('打开icon窗口')
      console.log(model)
      if (model === 1) {
        this.extIconsVisible = true
      } else {
        this.uniAppIconsVisible = true
      }
    },
    setIcon(val) {
      console.log('选中')
      console.log(val)
      this.currentIconModelValue = val
      this.prefixIcon = val
      if(this.form.isCustom === 1){
        this.form.iconType = val
      }else {
        this.form.iconType = val.substr(6)
      }
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.status === 1 ? '启用' : '禁用'
      updateAppMenu({ id: row.id, status: row.status }).then(() => {
        this.$modal.msgSuccess(text + '成功')
      })
    },
    /** 查询app菜单列表 */
    getList() {
      this.loading = true
      listAppMenu(this.queryParams).then(response => {
        this.menuList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        groupName: null,
        groupOrderNum: null,
        orderNum: null,
        path: null,
        query: null,
        isFrame: 0,
        isCache: 1,
        iconType: null,
        isCustom: 0,
        iconClass: '#ff8c00',
        status: 1,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加app菜单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      console.log(row)
      this.currentIconModelValue = row.iconType
      if(row.isCustom === 1){
        this.prefixIcon = row.iconType
      }else {
        this.prefixIcon = 'uniui-' + row.iconType
      }
      const id = row.id || this.ids
      getAppMenu(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改app菜单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppMenu(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAppMenu(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delAppMenu(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/menu/export', {
        ...this.queryParams
      }, `menu_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
